package com.jintian.smart.kernel.message.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jintian.smart.kernel.core.constant.SwaggerTagConst;
import com.jintian.smart.kernel.core.domain.RequestUser;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.kernel.core.domain.ValidateList;
import com.jintian.smart.base.api.message.form.MessageQueryForm;
import com.jintian.smart.base.api.message.form.MessageSendForm;
import com.jintian.smart.base.api.message.vo.MessageVO;
import com.jintian.smart.kernel.message.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import com.jintian.smart.kernel.common.context.ContextHolder;
import org.beetl.sql.core.page.PageResult;
import org.springframework.web.bind.annotation.*;

/**
 * 消息
 */
@RestController
@Tag(name = SwaggerTagConst.Support.MESSAGE)
@RequestMapping("/mms")
public class MessageController {
    @Resource
    private MessageService messageService;

    @Operation(summary = "分页查询我的消息")
    @PostMapping("/message/query")
    public ResponseDTO<PageResult<MessageVO>> query(@RequestBody @Valid MessageQueryForm queryForm) {
        return ResponseDTO.ok(messageService.query(queryForm));
    }

    @Operation(summary = "分页查询我的消息")
    @PostMapping("/message/queryMyMessage")
    public ResponseDTO<PageResult<MessageVO>> queryMyMessage(@RequestBody @Valid MessageQueryForm queryForm) {
        RequestUser user = ContextHolder.getUser();
        if (user == null) {
            return ResponseDTO.userErrorParam("用户未登录");
        }

        queryForm.setReceiverUserId(user.getUserId());
        queryForm.setReceiverUserType(user.getUserType().getValue());
        return ResponseDTO.ok(messageService.query(queryForm));
    }

    @Operation(summary = "查询未读消息数量")
    @GetMapping("/message/getUnreadCount")
    public ResponseDTO<Long> getUnreadCount() {
        RequestUser user = ContextHolder.getUser();
        if (user == null) {
            return ResponseDTO.userErrorParam("用户未登录");
        }
        return ResponseDTO.ok(messageService.getUnreadCount(user.getUserType(), user.getUserId()));
    }

    @Operation(summary = "更新已读")
    @GetMapping("/message/read/{messageId}")
    public ResponseDTO<String> updateReadFlag(@PathVariable Long messageId) {
        RequestUser user = ContextHolder.getUser();
        if (user == null) {
            return ResponseDTO.userErrorParam("用户未登录");
        }

        messageService.updateReadFlag(messageId, user.getUserType(), user.getUserId());
        return ResponseDTO.ok();
    }

    @Operation(summary = "通知消息-新建")
    @PostMapping("/message/sendMessages")
    @SaCheckPermission("system:message:send")
    public ResponseDTO<String> sendMessages(@RequestBody @Valid ValidateList<MessageSendForm> messageList) {
        messageService.sendMessage(messageList);
        return ResponseDTO.ok();
    }

    @Operation(summary = "通知消息-删除")
    @GetMapping("/message/delete/{messageId}")
    @SaCheckPermission("system:message:delete")
    public ResponseDTO<String> delete(@PathVariable Long messageId) {
        return messageService.delete(messageId);
    }
}
